remind = """
调用windows默认打印机压缩PDF文档
使用前需要
    - 修改 Adobe PDF 为默认打印机
    - 修改 Adobe PDF 的打印机首选项
        - 选择默认设置为: 高质量打印
        - 取消勾选: 查看Adobe PDF页面结果
        - 选择并创建: Adobe PDF输出文件夹
        pass: 批量打印的结果会放在此文件夹中
"""
import os
import sys
import re
import time
import win32api
import pywintypes  # 必须有，否则无法打包
from tkinter import Tk, filedialog, messagebox

BASE_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))
root = Tk()
root.withdraw()
continue_flag = messagebox.showinfo(title="请确认", message=remind)
file_path = filedialog.askdirectory()
file_path = file_path.replace('/', os.sep)
if not file_path or not os.path.isdir(file_path):
    messagebox.showinfo(title="", message="未选择文件夹，即将退出")


def gen_file_list(dir_path, pattern='.*\\.pdf$'):
    reg = re.compile(pattern, re.I)
    for parent_dir, _, filenames in os.walk(dir_path):
        for filename in filenames:
            if reg.search(filename):
                yield os.path.join(parent_dir, filename)


def master():
    file_list = gen_file_list(file_path)

    for file_item in file_list:
        process_item(file_item)
        time.sleep(2)


def process_item(filename):
    flag = win32api.ShellExecute(0, "print", filename, '', "", 0) >= 32
    if flag:
        print(filename + '\t--\t已发送打印机')
    else:
        print(filename + '\t--\t发送打印机失败')


if __name__ == '__main__':
    master()
